package com.atguigu.gulimall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class MyThreadConfig {

    /**
     *    int corePoolSize, 核心线程数
     *    int maximumPoolSize,最新线程数
     *    long keepAliveTime, 空闲线程剩余时间
     *    TimeUnit unit,时间单位
     *    BlockingQueue<Runnable> workQueue 阻塞队列
     *    ThreadFactory threadFactory, 线程工厂
     *    RejectedExecutionHandler handler 拒绝策略
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
        return new ThreadPoolExecutor(
                pool.getCoreSize(),
                pool.getMaxSize(),
                pool.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),//最多存储10万异步任务
                Executors.defaultThreadFactory(),//默认工厂
                new ThreadPoolExecutor.AbortPolicy());//抛弃策略
    }
}
